import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name="count",value = "/countServlet")
public class CountServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        jakarta.servlet.ServletContext servletContext=this.getServletContext();
        Integer count= (Integer) servletContext.getAttribute("count");
        if (servletContext.getAttribute("count")==null){
            count= 1;
            servletContext.setAttribute("count",count);
        }
        else servletContext.setAttribute("count",count+1);

        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html><head><meta charset=\"utf-8\">");
        out.println("<title>登录次数统计</title>");
        out.println("</head><body><h2>");
        out.println("您是第"+servletContext.getAttribute("count")+"位访客</h2></body></html>");

    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
